import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/modle/person/bloc/my_fuli_bloc.dart';
import 'package:shopping_group/modle/person/entity/fuli_sum_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/widget_utils.dart';
import 'cash_fulijin_page.dart';
import 'fulijuan_view_page.dart';

class ChangeFulijingViewPage extends StatefulWidget {
  @override
  _ChangeFulijingViewPageState createState() => _ChangeFulijingViewPageState();
}

class _ChangeFulijingViewPageState extends State<ChangeFulijingViewPage> {
  List<Widget> listViews = [
    FuLiJuanViewPage(),
    CashFulijinPage(),
  ];

  List<String> listStr = [
    '余额明细',
    '提现记录',
  ];
  int _index = 0;

  MyFuLijuanBloc _bloc;
  StreamSubscription _refresSubscription;
  FuliSumEntity _entity = FuliSumEntity();
  @override
  void initState() {
    super.initState();
    _bloc = MyFuLijuanBloc();
    _bloc.initSumDate.add(null);

    // _refresSubscription = eventBus.on<RefreshEvent>().listen((event) {
    //   _bloc.initDate.add(null);
    // });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '福利金',
          textColor: Colors.white,
          isHide: false,
          backgroundColor: Colours.blackground_609),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SafeArea(
          child: Container(
            decoration: BoxDecoration(
              color: Colours.title_white,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colours.blackground_609,
                      ),
                      child: Stack(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.centerLeft,
                            padding: EdgeInsets.symmetric(
                                horizontal: 16, vertical: 8),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  height: 16,
                                ),
                                WidgetUtils.getTextColorLineView('福利金余额',
                                    Colours.title_white, Dimens.FONT_SIZE_16),
                                _jfView(),
                              ],
                            ),
                          ),
                        ],
                      ),
                    )),
                SizedBox(
                  height: 8,
                ),
                Expanded(
                  child: bodyView(),
                  flex: 3,
                ),
                SizedBox(
                  height: 8,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _jfView() {
    return StreamBuilder(
      stream: _bloc.sendSumData,
      initialData: _entity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is FuliSumEntity) {
          _entity = object;
        }
        return Expanded(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Expanded(
              child: Container(
                  alignment: Alignment.center,
                  child: WidgetUtils.getTextColorView(
                    _entity.nowVal,
                    Colours.title_white,
                    Dimens.FONT_SIZE_30,
                  )),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    WidgetUtils.getTextColorView(
                      '总共领取',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                    WidgetUtils.getTextColorView(
                      '(${_entity.allGet})',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                  ],
                ),
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    WidgetUtils.getTextColorView(
                      '总共消费',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                    WidgetUtils.getTextColorView(
                      '(${_entity.allPay})',
                      Colours.title_white,
                      Dimens.FONT_SIZE_14,
                    ),
                  ],
                ),
              ],
            ),
          ],
        ));
      },
    );
  }

  Widget bodyView() {
    return Container(
        color: Colours.transparent,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Flex(
          direction: Axis.vertical,
          children: <Widget>[
            changeBottomView(),
            Expanded(
              child: listViews[_index],
            ),
          ],
        ));
  }

  Widget changeBottomView() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(top: 12),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded(
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                showChangeView(
                  0,
                  listStr[0],
                  _index == 0 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 0 ? Colours.blackground_609 : Colors.transparent,
                ),
                showChangeView(
                  1,
                  listStr[1],
                  _index == 1 ? Colours.blackground_609 : Colours.text_2222,
                  _index == 1 ? Colours.blackground_609 : Colors.transparent,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget showChangeView(
      int index, String title, Color color, Color dividerColor) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          setState(() {
            _index = index;
          });
        },
        child: Container(
          alignment: Alignment.center,
          color: Colors.transparent,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  WidgetUtils.getTextColorView(
                      title, color, Dimens.FONT_SIZE_14),
                  Container(
                    padding: const EdgeInsets.only(top: 12),
                    //margin: EdgeInsets.symmetric(horizontal: 56),
                    width: 64,
                    child: WidgetUtils.drawCodeDivider(
                        color: dividerColor, height: 2),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _refresSubscription?.cancel();
    super.dispose();
  }
}
